<?php

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Api\Traits\ProxyTrait;
use App\Models\AdminUser;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Log;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class WeChatController extends ApiController
{
    use AuthenticatesUsers, ProxyTrait;


    public function __construct()
    {
        
    }

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $app = app('wechat.official_account');
        $app->server->push(function($message){
            return "欢迎关注 87联盟！山哥sb 宇哥牛逼";
        });

        return $app->server->serve();
    }

    /**
     * 根据code获取微信网页授权access_token
     * 再获取用户信息
     * @return string
     */
    public function wechatlogin(Request $request)
    {
        Log::info($request->get('code'));
        Log::info($request->get('state'));

        $client = new Client();
        $url= "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd216f71c10a5c4d1&secret=eb0aba97819271ad8669508179b8161b"."&code=".$request->get('code')."&grant_type=authorization_code";
        Log::info($url);
        $respond = $client->request('GET', $url);
        /*Log::info($respond->getStatusCode());
        Log::info($respond->getBody());*/
        $obj = json_decode($respond->getBody());
        $openid = $obj->openid;
        $access_token = $obj->access_token;
        Log::info($openid);
        Log::info($access_token);

        $client_2 = new Client();
        $url_2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        Log::info($url_2);
        $respond_2 = $client->request('GET', $url_2);
        //Log::info($respond_2->getBody());
        Log::info($respond_2->getStatusCode());
        $obj_2 = json_decode($respond_2->getBody());
        Log::info($obj_2->openid);
        Log::info($obj_2->nickname);
        Log::info($obj_2->headimgurl);
        if(!empty($obj_2->openid)){
            $user = User::firstOrNew(['weixin_openid' => $obj_2->openid]);
            $user->head_image = $obj_2->headimgurl;
            $user->email = $obj_2->openid.'@126.com';
            $user->name = $obj_2->openid;
            $user->weixin_openid = $obj_2->openid;
            $user->weixin_head_image_path = $obj_2->headimgurl;
            $user->weixin_name = $obj_2->nickname;
            $user->city = $obj_2->city;
            $user->country = $obj_2->country;
            $user->sex = $obj_2->sex;
            $user->language = $obj_2->language;
            $user->enable = 'F';
            $user->is_admin = 'F';
            $user->last_login_at = Carbon::now();
            Log::info('----------------aaaaaaaaaaa');
            $user->weixin_session_key = md5($obj_2->openid.time().rand());
            Log::info($user->weixin_session_key);
            $user->save();
            return $this->success(['userinfo' => $user]);
        }else{
            return response()->json(['status' => 'fail', 'code' => '900', 'message' =>'微信授权失败']);
        }

        
    }
}
